﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json.Linq;
using Sparklings.JsonUtils;
using Sparklings.RayTracing;
using System;
using System.Collections.Generic;

namespace Sparklings.LevelElements
{
    public class SpotPlateform : Tile, IActivable
    {
        public SpotPlateform(Level l, int x, int y, RayColor cStart, RayColor cOther) :
            base(l, x, y, false)
        {
            lightSurfaces = new List<LightHittable>();
            lightSurfaces.Add(new LightHittable(PositionULCorner, PositionURCorner, true));
            lightSurfaces.Add(new LightHittable(PositionURCorner, PositionLRCorner, true));
            //lightSurfaces.Add(new LightHittable(PositionLRCorner, PositionLLCorner, true));
            lightSurfaces.Add(new LightHittable(PositionLLCorner, PositionULCorner, true));
            foreach (LightHittable hittable in lightSurfaces)
            {
                hittable.Hit += OnHit;
            }
            spot = new Spot(new Vector2(x + 0.5f, y + 0.5f), new Vector2(0, 1), cStart);
            spot.On = true;
            ColorStart = cStart;
            ColorOther = cOther;
        }
        public override void LoadGraphicsRelatedElements()
        {
            base.LoadGraphicsRelatedElements();
            BaseTexture = Level.ContentManager.Load<Texture2D>("spot_platform");
        }
        public override IList<LightHittable> LightSurfaces
        {
            get
            {
                return lightSurfaces;
            }
        }
        public override void DrawEmissive(Rendering.RenderContext rc)
        {
            if (spot.Color != RayColor.None)
            {
                rc.Draw(
                    Level.ContentManager.Load<Texture2D>("SpotLight1"),
                    PositionLLCorner + new Vector2(0.5f,(spot.HitDistance-0.5f)/2),
                    spot.Color.ToColor(),
                    1.0f,
                    spot.HitDistance-0.5f);
            }
        }
        public override bool IsPassableBySparkling(Sparkling c) { return false; }
        public override Newtonsoft.Json.Linq.JToken GetJsonData(JsonUtils.JsonOverrider converter)
        {
            var ret = new JObject();
            ret["color_start"] = converter.Write(spot.Color);
            ret["color_other"] = converter.Write((spot.Color == ColorStart) ? ColorOther : ColorStart);
            return ret;
        }
        public static Object ConstructFromData(JsonOverrider converter, JToken toConvert, Level level, int x, int y)
        {
            var ret = new SpotPlateform(
                level, x, y,
                converter.Read<RayColor>(toConvert["color_start"]),
                converter.Read<RayColor>(toConvert["color_other"]));
            return ret;
        }
        public Spot spot { get; protected set; }
        public RayColor ColorStart;
        public RayColor ColorOther;
        private IList<LightHittable> lightSurfaces;

        public bool Activate()
        {
            spot.Color = (spot.Color == ColorStart) ? ColorOther : ColorStart;
            return true;
        }

        public bool IsPlayerActivable { get; set; }

        public List<int> ActivatingInterruptors { get; set; }
    }
}
